package com.dong.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;

@Configuration // 配置类
@ComponentScan(value = "com.dong") // 扫描包
@EnableTransactionManagement
public class SpringConfig {

    @Bean
    public DriverManagerDataSource getDriverManagerDataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        // 设置4个参数
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://192.168.44.3:3306/test");
        dataSource.setUsername("root");
        dataSource.setPassword("");
        return dataSource;
    }
    @Bean
    public JdbcTemplate getJdbcTemplate(){
        JdbcTemplate jdbcTemplate = new JdbcTemplate(this.getDriverManagerDataSource());
        return jdbcTemplate;
    }

    //创建平台事务管理器对象
//    @Bean(name="transactionManager")
//    public PlatformTransactionManager createTransactionManager(){
//        DataSourceTransactionManager manager = new DataSourceTransactionManager(this.getDriverManagerDataSource());
//        return manager;
//    }
}